require 'rho/rhocontroller'
require 'helpers/browser_helper'
require 'base64'
class ImageController < Rho::RhoController
  include BrowserHelper

  def save_image
    data = @params["data"]
    
    path = Rho::RhoApplication.get_blob_path('canvas.png')
    File.open(path, "wb") do |file|
      file.write(Base64.decode64(data))
    end
    
    image = Image.new({'image_uri' => 'canvas.png', 'is_blob' => true})
    image.save  
    
    $image = image
    render :string => "ok"
  end
  

  def delete_image
    @image = $image = nil
    redirect :action => :index, :controller => :QADetail
  end

  def take_picture
    Camera::take_picture(url_for(:action => :camera_callback))
     redirect :action => :index, :controller => :QADetail
  end

  def select_picture
    Camera::choose_picture(url_for(:action => :camera_callback))
    redirect :action => :index, :controller => :QADetail
  end

  def camera_callback
    if @params['status'] == 'ok'
    $image = Image.new({'image_uri' => @params["image_uri"], 'is_blob' => true})
        
      WebView.navigate(url_for(:action => :index, :controller => :QADetail))
    else
      Alert.show_popup @params['status']
    end
  end
end
